﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdDrawProfileByPick.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureCmdIds.h"
#include "IUserTransaction.h"
#include "UiCommonDialog.h"
#include "IUiView.h"
#include "GbmpModelViewTypes.h"
#include "IModelLine.h"
#include "TransferOwnership.h"
#include "IModelView.h"
#include "IPlane.h"
#include "ICurve2d.h"
#include "OwnerPtr.h"
#include "PickCurveDrawProfileBehavior.h"
#include "IPlaneCurve3d.h"
#include "ProfileUsedModelLineUtils.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdDrawProfileByPick)

CmdDrawProfileByPick::CmdDrawProfileByPick() :CmdCreateElementByPick(ID_CMD_STRUCTURE_DRAW_PROFILE_BY_PICK)
{

}

bool CmdDrawProfileByPick::OnExecuteCommand()
{
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"创建轮廓线"), GBMP_TR(L"此视图下不支持创建轮廓线"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }

    return true;
}

void CmdDrawProfileByPick::SetupRibbonContextualPanel()
{
    
}

OwnerPtr<PickCurveDrawElementBehavior> CmdDrawProfileByPick::CreateActionDrawCurvePickBehavior()
{
    OwnerPtr<PickCurveDrawProfileBehavior> opProfileBehavior = NEW_AS_OWNER_PTR(PickCurveDrawProfileBehavior, UiDocumentViewUtils::GetCurrentDocument());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opProfileBehavior, L"opProfileBehavior为空",L"GDMPLab",L"2024-03-30");
    opProfileBehavior->SetOnCurvePickedCallback(PickCurveDrawElementBehavior::CreateOnCurvePickedCallback(&CmdDrawProfileByPick::OnCurvePicked, this));

    return TransferOwnership(opProfileBehavior);
}

bool CmdDrawProfileByPick::CreatePresetElement(ElementId& elementId)
{
    elementId = ElementId::InvalidID;
    return true;
}

bool CmdDrawProfileByPick::OnCurvePicked(IUiView* pCurrentView, OwnerPtr<ICurve2d> opCurve2d)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opCurve2d, L"opCurve2d为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制轮廓"));
    IModelLine* pIModelLine = nullptr;
    {
        OwnerPtr<IPlane> opWorkPlane = pModelView->GetWorkPlane();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opWorkPlane, L"opWorkPlane为空",L"GDMPLab",L"2024-03-30");
        OwnerPtr<IPlaneCurve3d> opCurve3d = IPlaneCurve3d::Create(opWorkPlane->GetCoordinate(), TransferOwnership(opCurve2d));
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opCurve3d, L"opCurve3d为空",L"GDMPLab",L"2024-03-30");

        ProfileUsedModelLineUtils::CreateModelLineUsedForProfile(pDoc, TransferOwnership(opCurve3d));
    }

    ut->Commit();
    return true;
}



